package com.itheima.controller;

import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.resources.MessageConstant;
import com.itheima.service.CheckGroupService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "/checkGroup",name = "检查组模块")
public class CheckGroupController {
    //注入service接口对象
    @Reference
    private CheckGroupService checkGroupService;

    @RequestMapping(value = "/addCheckGroup.do",name = "添加功能")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result addCheckGroup(@RequestBody CheckGroup checkGroup, @RequestParam Integer[] checkItemIds){

        try {
            //调用生产者 添加检查组
            checkGroupService.addCheckGroup(checkGroup,checkItemIds);
            //添加成功
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //添加失败
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }

    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @RequestMapping(value = "/findPageCheckGroup.do",name = "查询功能")
    public Result findPageCheckGroup(@RequestBody QueryPageBean queryPageBean){

        try {
            //调用生产者 查询检查组信息
            Map map=checkGroupService.findPageCheckGroup(queryPageBean);
            //添加成功
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            //添加失败
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping(value = "/echoCheckGroup.do",name = "回显检查项和已勾选检查项功能")
    public Result echoCheckGroup(@RequestParam Integer id){

        try {
            //调用生产者 查询检查组信息
            Map map=checkGroupService.echoCheckGroup(id);
            //回显成功
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            //回显失败
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping(value = "/updateCheckGroup.do",name = "修改检查组")
    public Result updateCheckGroup(@RequestBody CheckGroup checkGroup,@RequestParam Integer[] checkItemIds){

        try {
            //调用生产者 修改检查组信息
            checkGroupService.updateCheckGroup(checkGroup,checkItemIds);
            //修改成功
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //修改失败
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }

    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping(value = "/deleteCheckGroup.do",name = "逻辑删除检查组")
    public Result deleteCheckGroup(@RequestParam Integer id){

        try {
            //调用生产者 删除检查组信息
            checkGroupService.deleteCheckGroup(id);
            //修改成功
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //修改失败
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }

    @RequestMapping(value = "/findAllCheckGroup.do",name = "查询所有检查组")
    public Result findAllCheckGroup(){

        try {
            //调用生产者 查询检查组信息
            List<CheckGroup> checkGroupList= checkGroupService.findAllCheckGroup();
            //查询成功
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }
}
